package com.ruoyi.web.controller.basice_info;

import com.ruoyi.basice_info.domain.TbTeacher;
import com.ruoyi.basice_info.service.ITbTeacherService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 教研组人员设置Controller
 *
 * @author hongyi
 * @date 2023-12-08
 */
@RestController
@RequestMapping("/basice_info/teacher")
public class TbTeacherController extends BaseController
{
    @Autowired
    private ITbTeacherService tbTeacherService;

    /**
     * 查询教研组人员设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:teacher:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbTeacher tbTeacher)
    {
        startPage();
        List<TbTeacher> list = tbTeacherService.selectTbTeacherList(tbTeacher);
        return getDataTable(list);
    }

    /**
     * 导出教研组人员设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:teacher:export')")
    @Log(title = "教研组人员设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbTeacher tbTeacher)
    {
        List<TbTeacher> list = tbTeacherService.selectTbTeacherList(tbTeacher);
        ExcelUtil<TbTeacher> util = new ExcelUtil<TbTeacher>(TbTeacher.class);
        util.exportExcel(response, list, "教研组人员设置数据");
    }

    /**
     * 获取教研组人员设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('basice_info:teacher:query')")
    @GetMapping(value = "/{teacherId}")
    public AjaxResult getInfo(@PathVariable("teacherId") Long teacherId)
    {
        return success(tbTeacherService.selectTbTeacherByTeacherId(teacherId));
    }

    /**
     * 新增教研组人员设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:teacher:add')")
    @Log(title = "教研组人员设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbTeacher tbTeacher)
    {   addOrEdit(tbTeacher,"add");
        return toAjax(tbTeacherService.insertTbTeacher(tbTeacher));
    }

    /**
     * 修改教研组人员设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:teacher:edit')")
    @Log(title = "教研组人员设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbTeacher tbTeacher)
    {     addOrEdit(tbTeacher,"edit");
        return toAjax(tbTeacherService.updateTbTeacher(tbTeacher));
    }


    public void addOrEdit(TbTeacher tbTeacher, String type) {
        TbTeacher teacherCode = tbTeacherService.getTbTeacherByTeacherCode(tbTeacher.getTeacherCode());
        if (teacherCode!=null) {
            if(type.equals("add")) {
                throw new IllegalArgumentException("教师代码已存在");
            } else {
                if(!tbTeacher.getTeacherId().equals(teacherCode.getTeacherId())) {
                    throw new IllegalArgumentException("教师代码已存在");
                }
            }
        }
    }

    /**
     * 删除教研组人员设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:teacher:remove')")
    @Log(title = "教研组人员设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{teacherIds}")
    public AjaxResult remove(@PathVariable Long[] teacherIds)
    {
        return toAjax(tbTeacherService.deleteTbTeacherByTeacherIds(teacherIds));
    }
}
